﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MySQLStudio.BusinessLogic
{
    public class SqlExecutionResult
    {
        private DataTable m_resultsTable;
        private List<string> m_messages;
        private bool m_dataRead;
        private int m_numberOfRowsRead;        

        public bool DataRead
        {
            get { return m_dataRead; }
            set { m_dataRead = value; }
        }

        public int NumberOfRowsRead
        {
            get { return m_numberOfRowsRead; }
            set { m_numberOfRowsRead = value; }
        }

        public DataTable ResultsTable
        {
            get { return m_resultsTable; }
        }

        public List<string> Messages
        {
            get { return m_messages; }
            set { m_messages = value; }
        }

        public SqlExecutionResult()
        {
            this.m_resultsTable = new DataTable();
            this.m_messages = new List<string>();
        }

        public void AddColumn(DataColumn column)
        {
            this.m_resultsTable.Columns.Add(column);
        }

        public void AddMessage(string message)
        {
            if (!string.IsNullOrEmpty(message))
                this.m_messages.Add(message);
        }

        /// <summary>
        /// Extracts data from data reader to data table
        /// </summary>
        /// <param name="reader"></param>
        public void ReadData(IDataReader reader)
        {
            string columnName = string.Empty;

            if (this.m_resultsTable.Columns != null)
            {
                while (reader.Read())
                {
                    DataRow resultRow = this.m_resultsTable.NewRow();
                    for (int i = 0; i < this.m_resultsTable.Columns.Count; i++)
                    {
                        columnName = this.m_resultsTable.Columns[i].ColumnName;
                        resultRow[columnName] = reader[columnName];                        
                    }

                    this.m_resultsTable.Rows.Add(resultRow);
                    m_numberOfRowsRead++;
                }
            }
        }
    }
}
